home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.12 Dec 96 / Async I⁄O Code / Sources / DropShellAsync.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-11  |  7.8 KB  |  329 lines  |  [TEXT/CWIE]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DropShell.c
  5. **
  6. **   Description:    Main application code for the QuickShell
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Author    Description
  23. **    ---------    ------    ---------------------------------------------
  24. **    23 Jun    94    LDR        Implemented support for disk insertion events
  25. **    02 Feb    94    LDR        Updated for Final SDK & CodeWarrior a2
  26. **                        Removed the ResumeProc as per new Apple recommendations
  27. **    11 Dec 93    SCS        Universal Headers/UPPs (Phoenix 68k/PPC & PPCC)
  28. **                        Skipped System 6 compatible rev of DropShell source
  29. **    09 Dec 91    LDR        Added support for new "Select File…" menu item
  30. **                        Quit now sends AEVT to self to be politically correct
  31. **                        Added support for the new gSplashScreen
  32. **    24 Nov 91    LDR        Added support for the Apple Menu (duh!)
  33. **    29 Oct 91    SCS        Changes for THINK C 5
  34. **    28 Oct 91    LDR        Officially renamed DropShell (from QuickShell)
  35. **                        Added a bunch of comments for clarification
  36. **    06 Oct 91    MTC        Converted to MPW C
  37. **    09 Apr 91    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #ifndef __MWERKS__
  42. #include <Desk.h>
  43. #include <Dialogs.h>
  44. #include <Errors.h>
  45. #include <Files.h>
  46. #include <Fonts.h>
  47. #include <Memory.h>
  48. #include <Menus.h>
  49. #include <StandardFile.h>
  50. #include <TextEdit.h>
  51. #include <Types.h>
  52. #include <Windows.h>
  53. #endif
  54.  
  55. // #include <sioux.h>
  56.  
  57. #include "DSGlobals.h"
  58. #include "DSUserProcs.h"
  59. #include "DSAppleEvents.h"
  60.  
  61. #include "DropShell.h"
  62. #include "AsyncPrefsDialog.h"
  63.  
  64.  
  65.  
  66. Boolean        gDone, gOApped, gHasAppleEvents, gWasEvent;
  67. EventRecord    gEvent;
  68. MenuHandle    gAppleMenu, gFileMenu, gTestMenu;
  69. WindowPtr    gSplashScreen;
  70.  
  71. Boolean        gModalDialogActive;
  72. DialogPtr    gModalDialog;
  73.  
  74. extern         Str255    ResultStrings[5];
  75.  
  76. #ifdef MPW
  77. extern void _DataInit();    
  78. #endif
  79.  
  80.  
  81. #pragma segment Initialize
  82. void InitToolbox (void) 
  83. {
  84.  
  85. #ifdef MPW
  86.     UnloadSeg ((Ptr) _DataInit );
  87. #endif
  88.  
  89.     InitGraf ( &qd.thePort );
  90.     InitFonts ();
  91.     InitWindows ();
  92.     InitMenus ();
  93.     TEInit ();
  94.     InitDialogs (NULL);        // use of ResumeProcs no longer approved by Apple
  95.     InitCursor ();
  96.     FlushEvents ( everyEvent, 0 );
  97.     
  98.     // how about some memory fun! Two should be enough!
  99.     MoreMasters ();
  100.     MoreMasters ();
  101.     }
  102.  
  103. /*
  104.     Let's setup those global variables that the DropShell uses.
  105.     
  106.     If you add any globals for your own use,
  107.     init them in the InitUserGlobals routine in DSUserProcs.c
  108. */
  109. #pragma segment Initialize
  110. Boolean InitGlobals (void) 
  111. {
  112.     long aLong;
  113.  
  114.     gDone            = false;
  115.     gOApped            = false;    // probably not since users are supposed to DROP things!
  116.     gHasAppleEvents    = Gestalt ( gestaltAppleEventsAttr, &aLong ) == noErr;
  117.     gSplashScreen    = NULL;
  118.     gModalDialogActive = false;
  119.     gModalDialog = NULL;
  120.  
  121.     return(InitUserGlobals());    // call the user proc
  122. }
  123.  
  124. /*
  125.     Again, nothing fancy.  Just setting up the menus.
  126.     
  127.     If you add any menus to your DropBox - insert them here!
  128. */
  129. #pragma segment Initialize
  130. void SetUpMenus (void) {
  131.  
  132.     gAppleMenu = GetMenu ( kAppleNum );
  133.     AddResMenu ( gAppleMenu, 'DRVR' );
  134.     InsertMenu ( gAppleMenu, 0 );
  135.  
  136.     gFileMenu = GetMenu ( kFileNum );
  137.     InsertMenu ( gFileMenu, 0 );
  138.  
  139.     // Added 2/23/96 RDC
  140.     gTestMenu = GetMenu ( kTestNum );
  141.     InsertMenu ( gTestMenu, 0 );
  142.  
  143.     DrawMenuBar ();
  144. }
  145.  
  146. /*
  147.     This routine is called during startup to display a splash screen.
  148.     
  149.     This was recommend by the Blue Team HI person, John Sullivan, who
  150.     feels that all apps should display something so that users can easily
  151.     tell what is running, and be able to switch by clicking.  Thanks John!
  152. */
  153. #pragma segment Initialize
  154. void InstallSplashScreen(void) 
  155. {
  156.     gSplashScreen = GetNewWindow(128, NULL, (WindowPtr)-1L);
  157. }
  158.  
  159.  
  160. /*    --------------- Standard Event Handling routines ---------------------- */
  161. #pragma segment Main
  162. void ShowAbout () {
  163.     (void) Alert ( 128, NULL );
  164.     }
  165.  
  166.  
  167. #pragma segment Main
  168. void DoMenu ( long retVal ) {
  169.     short    menuID, itemID;
  170.     Str255    itemStr;
  171.  
  172.     menuID = HiWord ( retVal );
  173.     itemID = LoWord ( retVal );
  174.     
  175.     switch ( menuID ) {
  176.         case kAppleNum:
  177.             if ( itemID == 1 )
  178.                 ShowAbout ();    /*    Show the about box */
  179.             else
  180.             {
  181.                 GetItem(GetMHandle(kAppleNum), itemID, itemStr);
  182.                 OpenDeskAcc(itemStr);
  183.             }
  184.             break;
  185.             
  186.         case kFileNum:
  187.             if ( itemID == 1 )
  188.                 SelectFile();        // call file selection userProc
  189.             else
  190.                 SendQuitToSelf();    // send self a 'quit' event
  191.             break;
  192.         
  193.         case kTestNum:
  194.             if ( itemID == 1 ) {
  195.                 gModalDialog = OpenThisDialog();
  196.                 if (gModalDialog != NULL)
  197.                     gModalDialogActive = true;
  198.             }
  199.             break;
  200.  
  201.         default:
  202.             break;
  203.             
  204.         }
  205.     HiliteMenu(0);        // turn it off!
  206.     }
  207.  
  208.  
  209. #pragma segment Main
  210. void DoMouseDown ( EventRecord *curEvent ) {
  211.     WindowPtr    whichWindow;
  212.     short        whichPart;
  213.  
  214.     whichPart = FindWindow ( curEvent->where, &whichWindow );
  215.     switch ( whichPart ) {
  216.         case inMenuBar:
  217.             DoMenu ( MenuSelect ( curEvent->where ));
  218.             break;
  219.         
  220.         case inSysWindow:
  221.             SystemClick ( curEvent, whichWindow );
  222.             break;
  223.         
  224.         case inDrag:
  225.             {
  226.                 Rect    boundsRect = (*GetGrayRgn())->rgnBBox;
  227.                 DragWindow ( whichWindow, curEvent->where, &boundsRect );
  228.             }
  229.         default:
  230.             if (gModalDialogActive)
  231.                 DoDialogContent(gModalDialog, curEvent);
  232.             break;
  233.         }
  234.     }
  235.  
  236.  
  237. #pragma segment Main
  238. void DoKeyDown ( EventRecord *curEvent ) {
  239.     if ( curEvent->modifiers & cmdKey ) {
  240.         DoMenu ( MenuKey ((char) curEvent->message & charCodeMask ));
  241.     } else if (gModalDialogActive) {
  242.         short         itemHit;
  243.         DialogPtr    dlg;
  244.         DialogSelect(curEvent, &dlg, &itemHit);
  245.         }
  246.     }
  247.  
  248.  
  249. #pragma segment Main
  250. void main ( ) 
  251. {
  252.  
  253.     InitToolbox ();
  254.     if ( InitGlobals () ) {    // if we succeeding in initting self
  255.         if ( !gHasAppleEvents )
  256.             ErrorAlert ( kErrStringID, kCantRunErr, 0 );
  257.         else {
  258.             InitAEVTStuff ();
  259.             SetUpMenus ();
  260.             InstallSplashScreen ();
  261.             
  262.             while ( !gDone ) {
  263.                 gWasEvent = WaitNextEvent ( everyEvent, &gEvent, 0, NULL );
  264.                 if ( gWasEvent) { //  && !SIOUXHandleOneEvent(&gEvent)) {                    
  265.                     switch ( gEvent.what ) {
  266.                         case kHighLevelEvent:
  267.                             DoHighLevelEvent ( &gEvent );
  268.                             break;
  269.                             
  270.                         case mouseDown:
  271.                             DoMouseDown ( &gEvent );
  272.                             break;
  273.                             
  274.                         case keyDown:
  275.                         case autoKey:
  276.                             DoKeyDown ( &gEvent );
  277.                             break;
  278.  
  279.                         case diskEvt:
  280.                             if (HiWord(gEvent.message)) {
  281.                                 Point diskInitPt;
  282.                                 
  283.                                 diskInitPt.v = diskInitPt.h = 100;
  284.                                 DILoad();
  285.                                 DIBadMount(diskInitPt, gEvent.message);
  286.                                 DIUnload();
  287.                             }
  288.                             break;
  289.                         
  290.                         case updateEvt:
  291.                             if (gModalDialogActive) 
  292.                                 DoDialogUpdate(gModalDialog);
  293.                             else {
  294.                                 // Update our result window
  295.                                 int     count;
  296.                                 
  297.                                 BeginUpdate(gSplashScreen);
  298.                                 SetPort(gSplashScreen);
  299.                                 EraseRect(&(gSplashScreen->portRect));
  300.                                 if (*ResultStrings[0] != '\0') {
  301.                                     TextFace(0); TextFont(monaco); TextSize(10);
  302.                                     for (count = 0; count < 5; count++) {
  303.                                         MoveTo(3, 14 * (count + 1));
  304.                                         DrawString(ResultStrings[count]);
  305.                                     }
  306.                                 } else {
  307.                                     TextFace(bold); TextFont(monaco); TextSize(12);
  308.                                     MoveTo(3, 14);
  309.                                     DrawString("\pOpen a file to test read timings");
  310.                                 }
  311.                                 EndUpdate(gSplashScreen);
  312.                             }
  313.                         break;
  314.  
  315.                         case activateEvt:
  316.                             if (gModalDialogActive) 
  317.                                 DoDialogActivate(gModalDialog, gEvent.modifiers & 0x0001);
  318.                         break;
  319.  
  320.                         default:
  321.                             break;
  322.                     }
  323.                 }
  324.             }
  325.         }
  326.         DisposeUserGlobals();    // call the userproc to clean itself up
  327.     }
  328. }
  329.